package com.apanal.qlife.common.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 
 * 读取配置文件属性工具类<br>
 * 采用静态内部类实现单例模式(@value("${key}")方式不支持静态变量注入)
 * 
 * @author shuliangxing
 * 
 * @date 2014-12-12下午3:00:45
 */
public class PropertieUtil {

	private static final Logger logger = Logger.getLogger(PropertieUtil.class);

	private Properties properties;

	private PropertieUtil() {
		try {
			// 此种方式不支持通配符,如:*.properties
			// 生产
			properties = PropertiesLoaderUtils
					.loadAllProperties("prod.properties");
			// 测试
			if (properties == null || properties.isEmpty()) {
				properties = PropertiesLoaderUtils
						.loadAllProperties("test.properties");
			}
			// 开发
			if (properties == null || properties.isEmpty()) {
				properties = PropertiesLoaderUtils
						.loadAllProperties("dev.properties");
			}

			if (properties == null || properties.isEmpty()) {
				logger.warn("配置文件为空");
			}
		} catch (IOException e) {
			logger.error("初始化PropertieUtil异常" + e.getMessage(), e);
		}

	}

	private static class PropertieUtilFactory {
		private static final PropertieUtil instance = new PropertieUtil();
	}

	public static String getPropertie(String key) {
		return PropertieUtilFactory.instance.properties.getProperty(key);
	}

}
